package com.demo.action;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.business.base.Pager;
import com.demo.po.DemoBean;
import com.demo.service.DemoService;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/*
 * RestController 返回json数据
 */
@RestController
@RequestMapping(value = "/demo")
public class DemoController {

	@Resource
	private DemoService demoService;
	/*@Autowired
	private DemoProperties demoProperties;*/
	@Autowired
	@Qualifier("demoProperties")
	private Properties demoProperties;
	@Autowired
	@Qualifier("demo1Properties")
	private Properties demo1Properties;
	@RequestMapping(value = "/index")
	public String hello() {
		return "hello word!";
	}
	@RequestMapping(value = "/getProper")
	public String getProper() {
		return "testName:"+demoProperties.getProperty("name")+","+demo1Properties.getProperty("name");
	}

	/*
	 * 通过jackjson 格式化，如需隐藏格式化等操作可用注解
	 */
	@RequestMapping(value = "/getJson")
	public Map<String, Object> getJsonObject() {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("id", "1");
		m.put("name", "阿猫");
		m.put("code", "acode");
		return m;
	}

	/**
	 * 测试保存数据方法.
	 * 
	 * @return
	 */
	@RequestMapping("/save")
	public String save() {
		DemoBean d = new DemoBean();
		d.setId(UUID.randomUUID().toString());
		d.setName("Angel");
		//demoService.save(d);// 保存数据.
		return "ok.DemoController.save";
	}

	// 地址：http://localhost:8080/demo/getById?id=1
	@RequestMapping("/getById")
	public Map<String,Object> getById(String id) {
		return demoService.getEntity(id);
	}
	@RequestMapping("/getPage")
	public Pager<Map<String, Object>> getPage() {
		PageInfo<Map<String, Object>> pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect() {
			@Override
			public void doSelect() {
				demoService.getList(null);
			}
		});
		Pager<Map<String, Object>> result = new Pager<Map<String, Object>>();
		result.setPageNum(pageInfo.getPageNum());
		result.setPages(pageInfo.getPages());
		result.setPageSize(pageInfo.getPageSize());
		result.setSize(pageInfo.getSize());
		result.setTotal(pageInfo.getTotal());
		result.setList(pageInfo.getList());
		return result;
	}
}
